<?php


namespace App\HttpController;


use EasySwoole\Http\AbstractInterface\AbstractRouter;
use EasySwoole\Http\Message\Status;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
use EasySwoole\HttpAnnotation\Utility\Scanner;
use FastRoute\RouteCollector;

class Router extends AbstractRouter
{
    function initialize(RouteCollector $routeCollector)
    {
        $this->setGlobalMode(true);
        // 注册注解路由
        $scanner = new Scanner();
        $scanner->mappingRouter($routeCollector, EASYSWOOLE_ROOT . '/App/HttpController', 'App\HttpController');

        // 全局模式拦截
        $this->setMethodNotAllowCallBack(function (Request $request, Response $response) {
            ExceptionHandler::writeJson($response, Status::CODE_METHOD_NOT_ALLOWED, null, '未找到处理方法');
            return false;//结束此次响应
        });
        $this->setRouterNotFoundCallBack(function (Request $request, Response $response) {
            ExceptionHandler::writeJson($response, Status::CODE_NOT_FOUND, null, '未找到路由匹配');
            return false;//结束此次响应
        });
    }
}